package edu.scau.mis.web.controller.dashboard;

import edu.scau.mis.core.domain.AjaxResult;
import edu.scau.mis.dashboard.Service.ISaleItemService;
import edu.scau.mis.dashboard.dto.CategorySalesDTO;
import edu.scau.mis.dashboard.vo.PieDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sale")
public class SalesController {

    @Autowired
    private ISaleItemService saleItemService;

    /**
     * 获取各年份各月销售数据
     */
    @GetMapping("/yearly-monthly")
    public AjaxResult getYearlyMonthlySales() {
        Map<Integer, BigDecimal[]> data = saleItemService.getYearlyMonthlySales();
        return AjaxResult.success(data);
    }

    @GetMapping("/category-salesAmount")
    public AjaxResult getTopCategorySalesAmount(
            @RequestParam String startDate,
            @RequestParam String endDate) {
            LocalDate start = LocalDate.parse(startDate);
            LocalDate end = LocalDate.parse(endDate);
            List<CategorySalesDTO> result = saleItemService.getTopCategorySalesAmount(start, end);
            return AjaxResult.success(result);
        }


    @GetMapping("/category-salesQuantity")
    public AjaxResult getTopCategorySalesQuantity(
            @RequestParam String startDate,
            @RequestParam String endDate) {
        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);
        List<PieDataVo> result = saleItemService.getTopCategorySalesQuantity(start, end);
        return AjaxResult.success(result);
    }
}
